package gui;


import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;
import javax.swing.border.Border;

import Projektarbete.ContactTabListFiller;


public class StickyNotePanel extends javax.swing.JPanel {
	
	ContactTabListFiller filler;
	private int groupID;
	private ArrayList<String> messageList = new ArrayList<String>();
	private JLabel messageTextLabel;
	
    public StickyNotePanel(int id) {
    	filler = new ContactTabListFiller();
    	this.groupID = id;
        initComponents();
    }

    public int getGroupId(){
    	return groupID;
    }
   
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    	jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        messageArea = new javax.swing.JPanel();
        jPanel2.setOpaque(false);
        setOpaque(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        
       

        messageArea.setBackground(new java.awt.Color(255, 255, 255));
        messageArea.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout messageAreaLayout = new javax.swing.GroupLayout(messageArea);
        messageArea.setLayout(messageAreaLayout);
        messageAreaLayout.setHorizontalGroup(
            messageAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 446, Short.MAX_VALUE)
        );
        messageAreaLayout.setVerticalGroup(
            messageAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(messageArea, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(messageArea, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        
        

        
        getJPanelWithMessage();

    }// </editor-fold>
    
    public ContactTabListFiller getFiller(){
    	return filler;
    }
    
    
    
    public void getJPanelWithMessage() {
	     messageList = getFiller().getStickyNote((groupID));
		
		Iterator<String> iterator = messageList.iterator();

		while (iterator.hasNext()) {
			String nextMessage = iterator.next();
			putMessage(nextMessage);
		}
	}
   

	JPanel chatStamp;
    JTextArea messageTextArea;
    public void putMessage(String message){  	
    	
    	messageTextLabel = new JLabel(message);

		Border paddingBorder = BorderFactory.createEmptyBorder(5,10,5,10);
		
		messageTextLabel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(),paddingBorder));

		// Layouts
		ColumnLayout chatStampLayout = new ColumnLayout(0, 0, 0, 0);
		messageArea.setLayout(chatStampLayout);
		
		// Trolleri
		messageArea.setBackground(Color.WHITE);

		messageTextLabel.setFont(new Font("Arial", Font.PLAIN, 12));

		messageArea.add(messageTextLabel);
		messageArea.setMinimumSize(new Dimension(0, messageArea
				.getPreferredSize().height));
		jPanel3.setPreferredSize(new Dimension(0, messageArea
				.getPreferredSize().height));
        
        messageTextLabel.addMouseListener( new RightClicker(messageTextLabel.getText(), chatStamp));
        
        messageArea.scrollRectToVisible(new Rectangle(0, messageArea.getHeight()+100000,1 , 1));      
    }
    
    private class RightClicker extends MouseAdapter {  
    	
    	String message;
    	JPanel tempJPanel;
        public RightClicker(String text, JPanel jp) {
			this.message = text;
			this.tempJPanel = jp;
		}

		public void mousePressed( MouseEvent e) { 
            if ( e.isMetaDown() ) {
            	doPop(e, message, tempJPanel);
            }  
        }  
        
        private void doPop(MouseEvent e, String message, JPanel jp){
            PopUpDemo menu = new PopUpDemo(message, jp);
            menu.show(e.getComponent(), e.getX(), e.getY());
        }
    }  
    
    class PopUpDemo extends JPopupMenu {
        JMenuItem anItem;
        public PopUpDemo(String message, JPanel jp){
        	
            anItem = new JMenuItem("Delete seleted Sticky Note");
            anItem.addMouseListener( new DeleteStickyNote(message, jp));
            add(anItem);
        }
    }
    
    class DeleteStickyNote extends MouseAdapter{
    	
    	String message;
    	JPanel tempJPanel;
    	
    	public DeleteStickyNote(String message, JPanel jp) {
			this.message = message;
			this.tempJPanel = jp;
		}

		public void mousePressed(MouseEvent e){
    		deleteStickyNote(message, tempJPanel);
    		
    	}

    	public void deleteStickyNote(String message, JPanel jp){
    		System.out.println(message + "DELETED");
    		filler.removeStickyNote(groupID, message);
    		//messageList.remove(message);
    		messageArea.removeAll();
    		messageArea.updateUI();
    		
    		getJPanelWithMessage();
    		messageArea.updateUI();
    		jScrollPane1.updateUI();
    		//messageArea.remove(jp);
    		//messageArea.updateUI();
    	}
	
    }
    
    
    private void formComponentResized(java.awt.event.ComponentEvent evt) {                                      
        // TODO add your handling code here:
    	

//     messageArea.setPreferredSize(new Dimension(0,messageArea.getHeight()));
    	
        jPanel3.scrollRectToVisible(new Rectangle(0, messageArea.getHeight()+100000,1 , 1));      
    }                                     

    // Variables declaration - do not modify
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    javax.swing.JPanel messageArea;
    // End of variables declaration
	
	
	
}


